package de.mariusharbeck.saralentz.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@SuppressWarnings("serial")
public class Album implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	private String name;
	@Column(name = "DATE", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
	@Temporal(TemporalType.TIMESTAMP)
	private Date datum;
	@OneToMany(mappedBy = "album")
	private List<Bild> bilder;

	public Album(String name, List<Bild> bilder) {
		super();
		this.name = name;
		this.bilder = bilder;
	}

	public Album() {
		this("", new ArrayList<Bild>());
	}

	public String countBilder() {
		int i = bilder.size();
		return (i != 1) ? i + " Bilder" : i + " Bild";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Bild> getBilder() {
		return bilder;
	}

	public void setBilder(List<Bild> bilder) {
		this.bilder = bilder;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLinkName() {
		return this.getName().toLowerCase().replace(" ", "-");
	}

	public List<Bild> getVorschauBilder() {
		int max = 5;
		if (bilder.size() <= max) {
			return bilder;
		} else {
			List<Bild> bilder = new ArrayList<Bild>();
			for (int i = 0; i < max; i++) {
				bilder.add(this.bilder.get(i));
			}
			return bilder;
		}
	}

}
